//
//  GameDetailTableView.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/10/9.
//  Copyright © 2021 myTV. All rights reserved.
//

#import "GameDetailCommentTableView.h"
#import "GameCommentsTableViewCell.h"
#import "HoldbackAlertView.h"

@interface GameDetailCommentTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) BOOL tableCanScroll;

@property (nonatomic, strong) UIView *nodataView;
@property (nonatomic, strong) UIImageView *nodataPic;

@end

@implementation GameDetailCommentTableView

-(UIView *)nodataView
{
    if (!_nodataView) {
        _nodataView = [UIView new];
        _nodataView.hidden = YES;
        [self addSubview:_nodataView];
        _nodataView.backgroundColor = [UIColor clearColor];
        UIImageView *imgV = [[UIImageView alloc] init];
        imgV.frame = CGRectMake((SL_WINDOW_WIDTH - 80)/2.0, 80, 80, 80);
        imgV.contentMode = UIViewContentModeScaleAspectFit;
        [imgV setImage:[UIImage imageNamed:@"nodate"]];
        [_nodataView addSubview:imgV];
        _nodataPic = imgV;
    }
    return _nodataView;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.tableCanScroll = NO;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotification:) name:@"GameDetail_tableCanScroll" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableHeaderRefresh:) name:@"GameDetail_tableHeaderRefresh" object:nil];
        [self config];
    }
    return self;
}
-(void)config
{
    self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.backgroundColor = [UIColor clearColor];

    // setContentOffset 不准，加上这里就准了
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, CGFLOAT_MIN + 5)];
    
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.estimatedRowHeight = 130;
    
    [self registerNib:[UINib nibWithNibName:@"GameCommentsTableViewCell" bundle:nil] forCellReuseIdentifier:@"GameCommentsTableViewCell"];
        
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GameDetail_tableCanScroll" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"GameDetail_tableHeaderRefresh" object:nil];
}

-(void)setModelList:(NSArray *)modelList
{
    _modelList = modelList;
        
    // 暂未图
    if (modelList.count) {
        self.nodataView.hidden = YES;
    }else{
        self.nodataView.hidden = NO;
        
    }
    
    // 刷新
    [self reloadData];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GameCommentsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GameCommentsTableViewCell" forIndexPath:indexPath];
    cell.model = self.modelList[indexPath.row];
    VHWeakSelf
    cell.btnActionBlock = ^(NSInteger tag, CommentsModel * _Nonnull model) {
        VHStrongSelf
        [self btnAction:tag model:model];
    };
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:NSStringFromClass([GameCommentsTableViewCell class]) cacheByIndexPath:indexPath configuration:^(GameCommentsTableViewCell *cell) {
        cell.model = self.modelList[indexPath.row];
    }];
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"table -- %.2f", scrollView.contentOffset.y);
    
    if (!self.tableCanScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (scrollView.contentOffset.y <= 0) {
        _tableCanScroll = NO;
//        NSLog(@"-*- 通知 scrollView 可滑动");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"GameDetail_scrollCanScroll" object:nil userInfo:@{@"canScroll":@"1"}];
    }
    
}

#pragma mark - NSNotificationCenter
- (void)scrollNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if (canScroll.boolValue) {
        _tableCanScroll = YES;
    }
}

- (void)tableHeaderRefresh:(NSNotification *)notification
{
    if (self.tableHeaderReloadBlock) {
        self.tableHeaderReloadBlock();
    }
}

- (void)tableHeaderRefreshFinish
{
//    NSLog(@"-*- 通知 scroll 停止刷新");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GameDetail_scrollHeaderRefreshStop" object:nil userInfo:nil];
}

- (void)btnAction:(NSInteger)tag model:(CommentsModel *)model
{
    if (tag == 0) {
        //评论
    } else {
        // 弹窗
        HoldbackAlertView *alertView = [[NSBundle mainBundle] loadNibNamed:@"HoldbackAlertView" owner:self options:nil][0];
        alertView.frame = UIDevice.vg_rootWindow.bounds;
        VHWeakSelf
        alertView.sure = ^(NSInteger tag) {
            VHStrongSelf
            if (self.tableActionBlock) {
                self.tableActionBlock(tag, model);
            }
        };
        [UIDevice.vg_rootWindow addSubview:alertView];
    }
}




@end
